//
//  WEXOAuthViewController.m
//  microBlog_oc
//
//  Created by apple on 16/4/21.
//  Copyright © 2016年 Hill. All rights reserved.
//

//define this constant if you want to use Masonry without the 'mas_' prefix
#define MAS_SHORTHAND

//define this constant if you want to enable auto-boxing for default syntax
#define MAS_SHORTHAND_GLOBALS

#import <Masonry/Masonry.h>

#import "WEXOAuthViewController.h"
#import "WEXNetworkTools.h"

#import "WEXUserAccountViewModel.h"

@interface WEXOAuthViewController ()<UIWebViewDelegate>

@property (nonatomic, weak) UIWebView *webView;

@end

static NSString *WB_APP_KEY = @"1336075890";

static NSString *WB_REDIRECT_URI = @"http://www.baidu.com/";

static NSString *WB_APP_SECRET = @"f535e5d4033d624ccad804392593a7db";

@implementation WEXOAuthViewController


-(void)loadView{
    
    UIWebView *webView = [[UIWebView alloc] init];
    
    self.webView = webView;
    
    self.view = webView;
    
    webView.delegate = self;
    
    NSString *urlString = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",WB_APP_KEY,WB_REDIRECT_URI];
    
    NSURL *url = [NSURL URLWithString:urlString];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [webView loadRequest:request];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupUI];
}

# pragma mark -
# pragma mark 初始化界面
- (void)setupUI {

    
    self.title = @"登录";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(closeView)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"自动填充" style:UIBarButtonItemStylePlain target:self action:@selector(autoStuff)];
    
    
}


# pragma mark -
# pragma mark 按钮点击方法
- (void)closeView {
    
    [self.navigationController dismissViewControllerAnimated:NO completion:nil];
}

- (void)autoStuff {
    
}

# pragma mark -
# pragma mark 实现代理方法
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
//    判断request中的前缀为 WB_REDIRECT_URI
    if ([request.URL.absoluteString hasPrefix:WB_REDIRECT_URI]) {
        
//        判断是否包含“code=”
        if ([request.URL.query containsString:@"code="]) {
            
            //给str赋值
            NSString *str = request.URL.query;
            NSString *code = [str substringFromIndex:5];
            

            NSLog(@"code = %@", code);
            [[WEXUserAccountViewModel sharedViewMoel] loadAccessToken:code withFinished:^(BOOL isSuccess) {
                
                if (isSuccess) {
                 
                    [self.navigationController dismissViewControllerAnimated:NO completion:^{
                        
                        
                        [[NSNotificationCenter defaultCenter] postNotificationName:WEXChangeRootVCNotification object:nil];
                    }];
                    
                    NSLog(@"SUCCES");
                    
                }else{
                    
                    NSLog(@"登录失败");
                    
                }
                
                
            }];
        }
        
        return NO;
    }
    
    
    return YES;
}





@end
